﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Danielyan.Exif;
using System.Drawing;
using Microsoft.Win32;

namespace WindStyle.WindowsLiveWriter.Plugin.ExifInfo
{
    public static class Utilities
    {
        public static string GetExifInfoString(string file)
        {
            return GetExifInfoString(file, Option.CurrentSettings.GetTemplateByName(Option.CurrentSettings.DefaultTemplateName).HtmlTemplate);
        }

        public static string GetExifInfoString(string file, string htmlTemplate)
        {
            string imgTag = "<img src=\"{0}\"/>";
            return GetExifInfoString(file, htmlTemplate, imgTag);
        }

        public static string GetExifInfoString(string file, string htmlTemplate, Size imageSize)
        {

            string imgTag = string.Format("<img src=\"{0}\" style=\"width:{1}px;height:{2}px\" />",
                "{0}",
                imageSize.Width,
                imageSize.Height);
            return GetExifInfoString(file, htmlTemplate, imgTag);
        }

        private static string GetExifInfoString(string file, string htmlTemplate, string imgTag)
        {
            try
            {
                string html = htmlTemplate.Replace("*Image*", string.Format(imgTag, file));

                ExifTags tags = new ExifTags(file);

                while (html.IndexOf('*') != -1)
                {
                    int startPos = html.IndexOf('*');
                    int endPos = html.IndexOf('*', startPos + 1);
                    string placehoder = html.Substring(startPos, endPos - startPos + 1);
                    string tagName = placehoder.Substring(1, placehoder.Length - 2);
                    string targetString = string.Empty;
                    if (tags.ContainsKey(tagName))
                    {
                        if (tagName == "DateTimeOriginal")
                        {
                            string[] datetimes = tags[tagName].Value.Split(" ".ToCharArray());
                            if (datetimes.Length > 1)
                            {
                                targetString = datetimes[0].Replace(':', '-') + " " + datetimes[1];
                            }
                        }
                        else
                        {
                            targetString = tags[tagName].Value.Trim();
                        }
                    }
                    else if (tagName == "ImageUrl")
                    {
                        targetString = "file:///" + file.Replace("\\", "/");
                    }
                    html = html.Replace(placehoder, targetString);
                }

                return html;
            }
            catch
            {
                return string.Format(imgTag, file);
            }
        }

        public static Size GetDefaultImageSize(string blogId, string imgPath)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows Live\Writer\Weblogs\" + blogId + @"\ImageDefaults\ImageResizeRotate");
            if (key != null)
            {
                string sizeName = key.GetValue("DefaultImageSizeName", "Custom").ToString();
                if (sizeName == "Custom")
                {
                    return new Size(Convert.ToInt32(key.GetValue("DefaultImageWidth", 400)),
                        Convert.ToInt32(key.GetValue("DefaultImageHeight", 225)));
                }
                else
                {
                    return APIHelper.ScaleImageSizeName(sizeName, imgPath);
                }
            }
            return APIHelper.ScaleImageSizeName("Small", imgPath);
            //HtmlImageResizeDecoratorSettings.GetDefaultImageSize(this.LoadDefaultImageDecoratorsList().GetImageDecoratorSettings("ImageResizeRotate"));

            //HKEY_CURRENT_USER\Software\Microsoft\Windows Live\Writer\Weblogs\37492c53-ceba-480d-9f30-840d81755e21\ImageDefaults\ImageResizeRotate\DefaultImageSizeName=Small
        }
    }
}

